/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.login.DAO;

import cn.edu.nju.software.login.domain.User;
import cn.edu.nju.software.login.util.SessionPool;
import org.hibernate.Session;

/**
 *
 * @author han
 */
public class UserDAO 
{
    Session session;
    
    //插入一个新用户
    public boolean save(User user)
    {
        boolean result = false;
        
        try
        {
            session = SessionPool.getSession();
            session.beginTransaction();
            session.save(user);
            result = true;
        }catch(Exception e)
        {
            e.printStackTrace();
        }finally
        {
            session.getTransaction().commit();
            return result;
        }
    }
    
    //删除一个用户
    public boolean delete(User user)
    {
        boolean result = false;
        
        try
        {
            session = SessionPool.getSession();
            session.beginTransaction();
            session.delete(user);
            result = true;
        }catch(Exception e)
        {
            e.printStackTrace();
        }finally
        {
            session.getTransaction().commit();
            return result;
        }
    }
    
    //根据用户名得到存在的用户对象
    public User get(String userName)
    {
        User result = null;
        
        try
        {
            session = SessionPool.getSession();
            session.beginTransaction();
            result = (User)session.get(User.class, userName);
        }catch(Exception e)
        {
            e.printStackTrace();
        }finally
        {
            session.getTransaction().commit();
            return result;
        }
    }
    
    //更新指定用户（通过id）的信息
    public boolean update(User user)
    {
        boolean result = false;
        
        try
        {
            session = SessionPool.getSession();
            session.beginTransaction();
            session.update(user);
            result = true;
        }catch(Exception e)
        {
            e.printStackTrace();
        }finally
        {
            session.getTransaction().commit();
            return result;
        }
    }
}
